package com.zzyl.serve.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        cleanRedis();
        return this.save(nursingLevel) ? 1 : 0;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        cleanRedis();
        return this.updateById(nursingLevel) ? 1 : 0;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        cleanRedis();
        return this.removeByIds(Arrays.asList(ids)) ? ids.length : 0;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        cleanRedis();
        return this.removeById(id) ? 1 : 0;
    }

    /**
     * 查询所有护理等级信息
     *  第一种优化方法，使用redis字符串的缓存
     * @return
     */
    @Override
    public List<NursingLevelVo> listAll() {
        //首先先查询redis中是否有数据
        String levelList = redisTemplate.opsForValue().get(CacheConstants.NURSING_LEVEL_KEY);
        List<NursingLevelVo> nursingLevelVos = null;
        //如果有数据就转换一下直接返回
        if (!StrUtil.isBlank(levelList)) {
            nursingLevelVos = JSON.parseArray(levelList, NursingLevelVo.class);
        }
        //如果没有数据就查询数据库
        if (CollectionUtils.isEmpty(nursingLevelVos)) {
            List<NursingLevel> nursingLevels = nursingLevelMapper
                    .selectList(Wrappers.<NursingLevel>lambdaQuery()
                            .eq(NursingLevel::getStatus, 1));
            //将数据库查询出来的数据转换成vo
            if (!CollectionUtils.isEmpty(nursingLevels)) {
                nursingLevelVos = nursingLevels.stream().map(s -> {
                    NursingLevelVo nursingLevelVo = new NursingLevelVo();
                    BeanUtils.copyProperties(s, nursingLevelVo);
                    return nursingLevelVo;
                }).collect(Collectors.toList());
                //存到redis中
                redisTemplate.opsForValue().set(CacheConstants.NURSING_LEVEL_KEY,
                        JSON.toJSONString(nursingLevelVos));
            }
        }
        return nursingLevelVos;
    }


//    /**
//     * 查询所有护理等级信息
//     * 第二种优化方法，使用redis的List缓存
//     *
//     * @return
//     */
//    @Override
//    public List<NursingLevelVo> listAll() {
//        //首先先查询redis中是否有数据
//        List<String> levelList = redisTemplate.opsForList().range(CacheConstants.NURSING_LEVEL_KEY, 0, -1);
//        List<NursingLevelVo> nursingLevelVos = null;
//        //如果有数据就转换一下直接返回
//        if (!CollectionUtils.isEmpty(levelList)) {
//            nursingLevelVos = levelList.stream().map(s -> {
//                NursingLevelVo nursingLevelVo = JSON.parseObject(s, NursingLevelVo.class);
//                return nursingLevelVo;
//            }).collect(Collectors.toList());
//        }
//        //如果没有数据就查询数据库
//        if (CollectionUtils.isEmpty(nursingLevelVos)) {
//            List<NursingLevel> nursingLevels = nursingLevelMapper.selectList(Wrappers.<NursingLevel>lambdaQuery().eq(NursingLevel::getStatus, 1));
//            //将数据库查询出来的数据转换成vo
//            if (!CollectionUtils.isEmpty(nursingLevels)) {
//                nursingLevelVos = nursingLevels.stream().map(s -> {
//                    NursingLevelVo nursingLevelVo = new NursingLevelVo();
//                    BeanUtils.copyProperties(s, nursingLevelVo);
//                    return nursingLevelVo;
//                }).collect(Collectors.toList());
//                //存到redis中
//                redisTemplate.opsForList().leftPush(CacheConstants.NURSING_LEVEL_KEY, String.valueOf(nursingLevelVos));
//            }
//        }
//        return nursingLevelVos;
//    }

    /**
     * 清理redis缓存，防止数据不一致
     */
    public void cleanRedis() {
        redisTemplate.delete(CacheConstants.NURSING_LEVEL_KEY);
    }
}
